public class code3 {
    public String countAndSay(int n) {
        String ret = "1";
        for(int i=1;i<n;i++) {//解释n-1次得到最终的结果
            StringBuilder tmp = new StringBuilder();
            int len = ret.length();//注意这里right并不是<n
            for(int left=0,right=0;right<len;) {
                int count = 0;
                while(right<len && ret.charAt(left)==ret.charAt(right)) right++;//注意不要让right越界
                tmp.append(right-left);
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();//每一次解释的结果
        }
        return ret;
    }
}
